﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace winform_用户界面客户端
{
    public partial class DB_Manager : Form
    {

        /// <summary>
        /// 搜索到的所有数据库文件对象
        /// </summary>
        public static List<FileInfo> All_DB_File = new List<FileInfo>();


        public DB_Manager()
        {
            InitializeComponent();

        }

        public void MyOutPut(string text)
        {
            if (text == "")
                return;
            RTB_Log.Text += RTB_Log.Text == "" ? text : "\n" + text;
            RTB_Log.SelectionStart = RTB_Log.Text.Length;
            RTB_Log.ScrollToCaret();
        }

        /// <summary>
        /// 搜索指定文件夹内的数据库文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void button1_Click(object sender, EventArgs e)
        {
            //检测参数合法性
            if (Root_Path.Text == null)
            {
                MyOutPut("路径为空");
                return;
            }

            //获取数据库文件。先判断路径是文件夹还是文件
            FileInfo[] All_DB_FileInfo;
            if (Directory.Exists(Root_Path.Text))
            {
                DirectoryInfo root_Dir = new DirectoryInfo(Root_Path.Text);
                MyOutPut("根目录锁定成功:" + root_Dir.FullName);
                //递归搜索数据库文件
                All_DB_FileInfo = root_Dir.GetFiles(GlobalValue.DB_Name, SearchOption.AllDirectories);
            }else if(File.Exists(Root_Path.Text))
            {
                All_DB_FileInfo =new FileInfo[]{new FileInfo(Root_Path.Text)};
            }
            else
            {
                MyOutPut("路径错误");
                return;
            }

            if (All_DB_FileInfo.Length == 0)
                MyOutPut("没有搜索到相关文件");

            //All_DB_File.Clear();
            for (int i = 0; i < All_DB_FileInfo.Length; i++)
            {
                MyOutPut("第" + (i + 1).ToString() + "个文件：" + All_DB_FileInfo[i].FullName);
                //检测数据库路径是否已经存在
                bool check = false;
                foreach(FileInfo f in All_DB_File)
                {
                    if (f.FullName == All_DB_FileInfo[i].FullName)
                    {
                        check = true;
                        break;
                    }
                }
                if(check==false)
                    All_DB_File.Add(All_DB_FileInfo[i]);                
            }
            Update_LV_DB();

        }

        /// <summary>
        /// 更新数据库列表界面
        /// </summary>
        private void Update_LV_DB()
        {
            LV_DB.BeginUpdate();
            LV_DB.Items.Clear();
            for (int i = 0; i < All_DB_File.Count; i++)
            {
                ListViewItem l = new ListViewItem();
                l.Text = (i + 1).ToString();
                l.SubItems.Add(All_DB_File[i].Name);
                l.SubItems.Add(All_DB_File[i].DirectoryName);
                l.Selected = true;//将列表项标记为选中
                LV_DB.Items.Add(l);
            }
            LV_DB.EndUpdate();
        }

        /// <summary>
        /// 打开 “目标-标签编辑”窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //Program.map_Editor.Show();
            foreach (ListViewItem L in LV_DB.SelectedItems)
            {
                if (L.SubItems[2].Text[L.SubItems[2].Text.Length - 1] != '\\')
                    GlobalValue.OpenDB(L.SubItems[2].Text + "\\" );
                else
                    GlobalValue.OpenDB(L.SubItems[2].Text);
            }

            Program.ShowWindow(Program.map_Editor);
            if (LV_DB.SelectedItems.Count == 1)
            {
                Program.map_Editor.Watcher_Path.Text= LV_DB.SelectedItems[0].SubItems[2].Text+"\\";
            }
        }

        /// <summary>
        /// 按钮“打开数据库”，打开“按标签搜索”窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {            
            if(LV_DB.SelectedItems.Count == 0)
            {
                MyOutPut("没有选择数据库");
                return;
            }
            List<string> dbPaths = new List<string>();
            
            foreach(ListViewItem L in LV_DB.SelectedItems)
            {
                if(L.SubItems[2].Text[L.SubItems[2].Text.Length - 1]!='\\')
                    dbPaths.Add(L.SubItems[2].Text + "\\" + L.SubItems[1].Text);
                else
                    dbPaths.Add(L.SubItems[2].Text + L.SubItems[1].Text);
            }            
            MyOutPut(string.Join("\n", dbPaths));
            Program.main_Form.RTB_DB_Path.Text = string.Join("\n",dbPaths);
            Program.main_Form.RTB_Open_DB_Click(sender,e);
            Program.main_Form.ShowDialog();
        }

        private string old_path = "";
        private void DB_Manager_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                old_path = Root_Path.Text;
                Root_Path.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
                e.Effect = DragDropEffects.Link;                
            }
        }

        private void DB_Manager_DragDrop(object sender, DragEventArgs e)
        {
            MyOutPut("开始扫描文件夹");
            old_path = Root_Path.Text;
            button1_Click(sender, e);
        }

        private void DB_Manager_DragLeave(object sender, EventArgs e)
        {
            Root_Path.Text = old_path;
        }

        private void btn_ClearList_Click(object sender, EventArgs e)
        {
            LV_DB.Items.Clear();
            All_DB_File.Clear();
        }

        private void BTN_BaiduAI_Click(object sender, EventArgs e)
        {
            Form_Baidu form_Baidu = new Form_Baidu();
            form_Baidu.Show();
               
        }
    }
}
